<template>
  <div class="app-container">
    <div class="head-container">
      <el-input v-model="taskName" clearable placeholder="输入名称搜索" maxlength="10" style="width: 200px;"
                class="filter-item" @keyup.enter.native="init"/>
      <el-button class="filter-item" size="mini" type="success" icon="el-icon-search" @click="init">搜索</el-button>
      <el-button
        class="filter-item"
        size="mini"
        type="primary"
        icon="el-icon-plus"
        @click="add">新增
      </el-button>
    </div>
    <el-table v-loading="loading" :data="data" size="small" style="width: 100%;" border>
      <el-table-column
        type="index"
        label="序号"
        align="center"
        width="60">
      </el-table-column>
      <el-table-column prop="taskName" label="任务名称" align="center" />
      <el-table-column prop="taskDesc" label="任务描述" align="center" show-overflow-tooltip/>
      <el-table-column prop="startTime" label="开始时间" align="center" width="150"/>
      <el-table-column prop="endTime" label="结束时间" align="center" width="150"/>
      <el-table-column prop="checkName" label="任务考核" align="center" width="200"/>
      <el-table-column label="任务状态" align="center" width="100">
        <template slot-scope="scope">
          <div v-for="item in dicts" :key="item.value">
            <el-tag v-if="scope.row.status === item.value-0" :type="scope.row.status ? '' : 'info'">{{ item.label }}
            </el-tag>
          </div>
        </template>
      </el-table-column>
      <el-table-column prop="duration" label="任务时长(分钟)" align="center"  width="150">
        <template slot-scope="scope">
          <span>{{formatSeconds(scope.row.duration)}}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="150" align="center">
        <template slot-scope="scope">
          <el-button size="mini" type="primary" icon="el-icon-edit" @click="edit(scope.row)"/>
          <el-popover
            :ref="scope.row.id"
            placement="top"
            width="180">
            <p>确定删除本条数据吗？</p>
            <div style="text-align: right; margin: 0">
              <el-button size="mini" type="text" @click="$refs[scope.row.id].doClose()">取消</el-button>
              <el-button :loading="delLoading" type="primary" size="mini" @click="subDelete(scope.row.id)">确定
              </el-button>
            </div>
            <el-button slot="reference" type="danger" icon="el-icon-delete" size="mini"/>
          </el-popover>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      :total="total"
      :current-page="page + 1"
      style="margin-top: 8px;"
      layout="total, prev, pager, next, sizes"
      @size-change="sizeChange"
      @current-change="pageChange"/>
  </div>
</template>

<script>
  import initData from '@/mixins/initData'
  import { del } from '@/api/business/task'
  import { formatSeconds } from '@/utils/index'
  import initDict from '@/mixins/initDict'

  export default {
    name: 'index',
    mixins: [initData, initDict],
    data() {
      return {
        taskName: '',
        delLoading: false
      }
    },
    created() {
      this.$nextTick(() => {
        this.getDict('publish_status')
        this.init()
      })
    },
    methods: {
      formatSeconds,
      beforeInit() {
        this.url = 'task/page-list'
        this.params = { page: this.page, size: this.size, taskName: this.taskName }
        return true
      },
      add() {
        this.$router.push({ path: '/home/taskAdd' })
      },
      edit(row) {
        this.$router.push({
          path: '/home/taskAdd',
          query: { id: row.id }
        })
      },
      subDelete(id) {
        this.delLoading = true
        del(id).then(res => {
          this.$notify({
            title: '删除成功',
            type: 'success',
            duration: 2500
          })
          this.init()
        })
        this.delLoading = false
        this.$refs[id].doClose()
      }
    }
  }
</script>
